﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.LLBLGen.Pro.ORMSupportClasses;
using MyHealthOnline.Dal.Generated.CollectionClasses;
using MyHealthOnline.Dal.Generated.HelperClasses;
using MyHealthOnline.Dal.Generated.EntityClasses;

namespace MyHealthOnline.Business
{
    public class SpecialistBusiness
    {
        public static SpecialistCollection GetSameDisciplineSpecialists(string specialistId)
        {
            PredicateExpression filter = new PredicateExpression(SpecialistFields.Id == specialistId);
            RelationCollection relColl = new RelationCollection();
            relColl.Add(SpecialistEntity.Relations.DisciplineEntityUsingDisciplineId);
            relColl.Add(DisciplineEntity.Relations.SpecialistEntityUsingDisciplineId).CustomFilter= filter;

            SpecialistCollection specialistColl = new SpecialistCollection();
            specialistColl.GetMulti(null, 0, null, relColl);
            return specialistColl;
        }

        public static SpecialistCollection GetSpecialists()
        {
            SpecialistCollection specColl = new SpecialistCollection();
            specColl.GetMulti(null);
            return specColl;
        }

        public static void CreateSpecialist
                (string name, string password, string code, string organization, string discipline, 
                string email, string address, string city, string country, string phone, string birthday, string gender)
        {
            SpecialistEntity spec = new SpecialistEntity
            {
                Name=name,
                Password=password,
                Id=code,
                Organization=organization,
                DisciplineId= Convert.ToInt32 (discipline),
                Email=email,
                Address=address,
                City=city,
                Country=country,
                Phone=phone,
                Birthday=birthday,
                Sex=gender
            };
            spec.Save();
        }

        public static void DeleteSpecialist(string specId)
        {
            PredicateExpression filter = new PredicateExpression(SpecialistFields.Id == specId);
            SpecialistCollection specColl = new SpecialistCollection();
            specColl.DeleteMulti(filter);
        }

        public static void UpdateSpecialist(string id, string name, string password, string organization, 
            string discipline, string email, string address, string city, string country, string phone,
            string birthday, string sex, int disciplineId)
        {
            if (disciplineId == 0)
                return;
            PredicateExpression filter = new PredicateExpression(SpecialistFields.Id == id);
            SpecialistCollection specList = new SpecialistCollection();
            specList.GetMulti(filter);
            if (specList != null && specList.Count > 0)
            {
                SpecialistEntity spec= specList.ElementAt(0);
                spec.Name = name;
                spec.Password = password;
                spec.Organization = organization;
                spec.DisciplineId =disciplineId;
                spec.Email = email;
                spec.Address = address;
                spec.City = city;
                spec.Country = country;
                spec.Phone = phone;
                spec.Birthday = birthday;
                spec.Sex = sex;
                spec.Save();
            }
        }
    }
}
